library FlameStrikeAbility initializer Init requires entities, utils, SpellBar
    /*
    --------------------------------------------------------------------------
    # 烈焰风暴
    --------------------------------------------------------------------------
    */

    globals
        public integer ABILITY_ID = 'AHfs'
    endglobals


    private function SpellAction takes Spell spell returns nothing
        local real x = spell.m_targetX
        local real y = spell.m_targetY
        local real range = spell.m_rangeEffect
        local UnitGroup gd
        local Link link
        local Unit ufd

        set gd = UnitGroup.fromArea(x, y, range)

        set link = gd.first
        loop
            exitwhen (link <= 0)
            set ufd = Unit(link.data)

            if IsWidgetAlive(ufd.m_unit) /* 存活的
            */then
                call spell.damageUnit(ufd.m_unit)
            endif
            set link = link.next
        endloop

        call gd.destroy()
    endfunction

    private function anon_edd34f52af7a takes nothing returns nothing
        local Spell spell = GetTimerData(GetExpiredTimer())

        if spell.m_count > 0 then
            call SpellAction(spell)
            set spell.m_count = spell.m_count - 1
        else
            call spell.destroy()
        endif
    endfunction

    private function SpellPrepare1 takes Spell spell returns nothing
        local integer level = GetUnitAbilityLevel(spell.m_unit, ABILITY_ID)

        if level == 1 then
            set spell.m_amount = 15 //每波伤害
        elseif level == 2 then
            set spell.m_amount = 26.67 //每波伤害
        elseif level == 3 then
            set spell.m_amount = 36.67 //每波伤害
        endif

        set spell.m_structureAmountRate = 0.75 //建筑伤害参数
        set spell.m_rangeEffect = 200 //影响范围
        set spell.m_interval = 0.33 //波次间隔
        set spell.m_ignoreAlliance = true //不分敌我
        set spell.m_count = R2I(3.67 / spell.m_interval)

        set spell.m_attackType = ATTACK_TYPE_MAGIC
        set spell.m_damageType = DAMAGE_TYPE_FIRE
        set spell.m_damageEffPath = null

        call TimerStart(spell.getInnerTimer(), spell.m_interval, true, function anon_edd34f52af7a)
    endfunction

    private function SpellPrepare2 takes Spell spell returns nothing
        local integer level = GetUnitAbilityLevel(spell.m_unit, ABILITY_ID)

        //每秒伤害
        if level == 1 then
            set spell.m_amount = 4
        elseif level == 2 then
            set spell.m_amount = 6
        elseif level == 3 then
            set spell.m_amount = 8
        endif

        set spell.m_structureAmountRate = 0.75 //建筑伤害参数
        set spell.m_rangeEffect = 200 //影响范围
        set spell.m_interval = 1.0 //波次间隔
        set spell.m_ignoreAlliance = true //不分敌我
        set spell.m_count = 10

        set spell.m_attackType = ATTACK_TYPE_MAGIC
        set spell.m_damageType = DAMAGE_TYPE_FIRE
        set spell.m_damageEffPath = null

        call TimerStart(spell.getInnerTimer(), spell.m_interval, true, function anon_edd34f52af7a)
    endfunction

    private function SpellFinish takes nothing returns nothing
        local unit u = SpellEvent.CastingUnit
        local real x = SpellEvent.TargetX
        local real y = SpellEvent.TargetY
        local Spell spell

        set spell = Spell.createNew()
        set spell.m_unit = u
        set spell.m_targetX = x
        set spell.m_targetY = y
        call SpellPrepare1(spell)

        set spell = Spell.createNew()
        set spell.m_unit = u
        set spell.m_targetX = x
        set spell.m_targetY = y
        call SpellPrepare2(spell)

        set u = null
    endfunction

    private function SpellEndCast takes nothing returns nothing
        local Spell spell = Spell.get(SpellEvent.CastingUnit, ABILITY_ID)
        call SpellBar.get(spell.m_unit).destroy()
        call spell.destroy()
    endfunction

    private function SpellChannel takes nothing returns nothing
        local Spell spell
        local SpellBar spellBar

        if not (IsWidgetAlive(SpellEvent.CastingUnit)) then
            return
        endif

        set spell = Spell.create(SpellEvent.CastingUnit, ABILITY_ID)
        set spell.m_timeCast = 1.67

        set spellBar = SpellBar.create(SpellEvent.CastingUnit)
        call spellBar.start(spell.m_timeCast, -60.0)
    endfunction

    private function Init takes nothing returns nothing
        call RegisterSpellChannelResponse(ABILITY_ID, SpellChannel)
        //call RegisterSpellEffectResponse(ABILITY_ID, SpellEffect)

        call RegisterSpellEndCastResponse(ABILITY_ID, SpellEndCast)
        call RegisterSpellFinishResponse(ABILITY_ID, SpellFinish)
    endfunction

endlibrary
